package com.smashcrush.integration;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.junit.Test;

import com.smashcrush.client.rest.ClientBrowserService;
import com.smashcrush.model.BrowserType;
import com.smashcrush.model.ExecuteScriptParameters;

public class GoogleSuggest {

	@Test
	public void something() {
		RegisterBuiltin.register(ResteasyProviderFactory.getInstance());

		ClientBrowserService client = ProxyFactory.create(
				ClientBrowserService.class, "http://localhost:8080");

		final String token = "abc";

		String sessionId = client.openSession(token, BrowserType.Chrome,
				"google.com");

		InputStream is = GoogleSuggest.class
				.getResourceAsStream("GoogleSuggest.groovy");
		String script = null;
		try {
			script = IOUtils.toString(is);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		ExecuteScriptParameters params = new ExecuteScriptParameters();
		params.setScript(script);

		Map<String, String> ret = client
				.executeScript(token, sessionId, params);

		for (String key : ret.keySet()) {
			System.out.println(key + " : " + ret.get(key));
		}

	}

}
